<template>
  <Modal
    ref="modal"
    title="快递追踪"
    width="400px"
    :before-show="beforeShow"
  >
    <template #reference>
      <slot />
    </template>
    <el-form
      ref="form"
      class="set-form"
      label-width="120px"
      :model="form"
    >
      <el-form-item
        label="供应商"
        prop="supplier"
      >
        <el-select
          v-model.trim="form.supplier"
          clearable
        >
          <el-option
            v-for="item in ['TRACK718', 'TRACK51', 'AFTERSHIP']"
            :key="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="text-center">
        <el-button @click="() => $refs.modal.hide()">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="handleSave"
        >
          确定
        </el-button>
      </div>
    </template>
  </Modal>
</template>

<script>
import { saveExpressTrack } from '@/api/express'
import { cloneDeep } from 'lodash-es'
import { isSuccess } from '@/utils/common'

export default {
    components: {
        Modal: () => import('@/components/Modal'),
    },
    props: {
        value: {
            type: Object,
            default() {
                return {}
            },
        },
    },
    data() {
        return {
            form: {},
        }
    },
    watch: {},
    created() {},
    methods: {
        async beforeShow() {
            await this.getDetail()
        },
        async getDetail() {
            this.form = cloneDeep(this.value)
        },
        async handleSave() {
            const valid = await this.$refs.form.validate()
            if (valid) {
                const res = await saveExpressTrack(this.form)
                if (isSuccess(res)) {
                    this.$message.success('修改成功!')
                    this.$store.dispatch('express/trackList')
                    this.$refs.modal.hide()
                }
            }
        },
    },
}
</script>
<style scoped>
  .set-form {
    height: 70px;
  }
</style>